package com.os.controller;

import com.os.model.Product;
import com.os.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
public class OsController {

    @Autowired
    private ProductService productService;

    /**
     * 批量插入数据
     * @return
     * @throws IOException
     */
    @RequestMapping("/insert")
    public String insert() throws IOException {

        productService.saveProduct();

        return "1";
    }

    /**
     * 通过条件查询
     * @param name
     * @return
     * @throws IOException
     */
    @RequestMapping("/get")
    public List<Product> get(String name) throws IOException {
        return productService.get(name);
    }

    /**
     * 默认查询10条数据
     * @return
     * @throws IOException
     */
    @RequestMapping("/getAll")
    public Integer getAll() throws IOException {
        return productService.getAll();
    }

    /**
     * 分页查询：注意from + size < 10000
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws IOException
     */
    @RequestMapping("/page")
    public List<Product> page(@RequestParam("no") int pageNumber, @RequestParam("size") int pageSize) {
        return productService.page(pageNumber, pageSize);
    }

    /**
     * 查询索引的文档数
     * @return
     * @throws IOException
     */
    @RequestMapping("/count")
    public Long count() throws IOException {
        return productService.count();
    }

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping("/scroll")
    public List<Product> scroll(@RequestParam("no") int pageNumber, @RequestParam("size") int pageSize) throws Exception {
        return productService.scroll(pageNumber, pageSize);
    }

    /**
     * 分页查询：推荐
     * 对数据量进行了判断
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping("/scroll1")
    public List<Product> scroll1(@RequestParam("no") int pageNumber, @RequestParam("size") int pageSize) {
        return productService.scroll1(pageNumber, pageSize);
    }

    /**
     * 导出全部数据
     * @return
     * @throws Exception
     */
    @RequestMapping("/export")
    public List<Product> export() {
        return productService.export();
    }


}
